<?php

namespace Admin\Controller;


class ProductController extends CommonController
{
    private $product;
    private $gallery;
    var $category;

    function __construct()
    {
        parent::__construct();
        $this->product = M('Product');
        $this->gallery = M('Gallery');
        $this->category = M('Category');
        $this->categories = $this->all_categories();


    }


    /***
     * 查出所有分类
     */
    private function all_categories()
    {
        $Category = M('Category');
        $categories = $Category->where("parent_id = 0")->select();
        foreach ($categories as $key => $value) {
            $children = $Category->where("parent_id = $value[id]")->select();
            $categories[$key]['children'] = $children;
        }
        return $categories;
    }

    /***
     * 商品管理首页
     */
    public function index()
    {
        $Product = M('Product');
        $Category = M('Category');

        $name = I('get.name');
        $category_id = I('get.category_id');

        $map = [];
        if (isset($name)) {
            $map['name'] = array('like', '%' . $name . '%');//按商品名称
        }
        if($category_id != ""){
            $map['category_id'] = $category_id;
        }

        $count = $this->product->where("status = 1")->count(); //查出满足要求的记录数
        $Page = new \Common\Util\Page($count, 5);// 实例化分页类 传入总记录数和每页显示的记录数
        $products = $Product->where($map)->where("status = 1")->order('sort_order')->
        limit($Page->firstRow . ',' . $Page->listRows)->select();

        foreach ($products as &$product) {
            $product['category'] = $Category->where("id = '$product[category_id]'")->find();
        }
        $show = $Page->show();//分页显示输出
        $datas = array('is_onsale','is_top','is_recommend','is_hot','is_new');


        $this->assign('page', $show);// 赋值分页输出
        $this->assign('count', $count);
        $this->assign('datas', $datas);
        $this->assign('products', $products);
        $this->display();
    }


    /***
     * 商品增加
     */
    public function add()
    {
        $Product = D('Product');
        if (IS_POST) {
            if (!$Product->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($Product->getError(), U('add'));
            } else {
                // 验证通过 可以进行其他数据操作
                $Product->create();
                $id = $Product->add();
                if ($id) {
                    $this->success('新增商品成功', U('index'));
                }
                $galleries = I('post.gallery');
                foreach ($galleries as $v) {
                    $gallery['img'] = $v;
                    $gallery['product_id'] = $id;
                    $this->gallery->where("product_id = '$id'")->add($gallery);

                }
            }
        } else {
            $this->display();
        }
    }

    /***
     * 编辑商品
     */
    public function edit()
    {
        $Product = D('Product');
        $id = I('get.id');
        if (IS_POST) {
            if (!$Product->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($Product->getError(), U('add'));
            } else {
                // 验证通过 可以进行其他数据操作
                $Product->create();
                $galleries = I('post.gallery');
                $result = $this->gallery->where("product_id = '$id'")->select();
                if ($result) {
                    //有图片更新图片
                    foreach ($galleries as $v) {
                        $gallery['img'] = $v;
                        $gallery['product_id'] = $id;
                        $this->gallery->where("product_id = '$id'")->save($gallery);
                    }
                } else {
                    //没有图片新增图片
                    foreach ($galleries as $v) {
                        $gallery['img'] = $v;
                        $gallery['product_id'] = $id;
                        $this->gallery->add($gallery);
                    }
                }
                $Product->where("id = '$id'")->save();
                $this->success('编辑成功', U('index'));
            }
        } else {
            //查出当前要编辑的信息
            $product = $this->product->where("id = '$id'")->find();
            //查出当前要编辑的相册
            $galleries = $this->gallery->where("product_id = '$id'")->select();
            $this->assign(compact('product', 'galleries'));
            $this->display();
        }
    }

    /***
     * 回收站首页
     */
    public function recycle()
    {
        $Product = M('Product');
        $Category = M('Category');


        $count = $Product->where("status = 0")->count(); //查出满足要求的记录数
        $Page = new \Common\Util\Page($count, 5);// 实例化分页类 传入总记录数和每页显示的记录数
        $products = $Product->where("status = 0")->
        limit($Page->firstRow . ',' . $Page->listRows)->select();

        foreach ($products as &$product) {
            $product['category'] = $Category->where("id = '$product[category_id]'")->find();
        }
        $show = $Page->show();//分页显示输出


        $this->assign('page', $show);// 赋值分页输出
        $this->assign('count', $count);
        $this->assign('products', $products);
        $this->display();
    }

    /***
     * ajax多选回收
     */
    public function recycle_all()
    {
        if (IS_AJAX) {
            $ids = I('post.ids');
            foreach ($ids as $v) {
                $this->product->where("id = $v")->setField("status", 0);
            }
        }
    }

    /***
     * ajax多选删除
     */
    public function delete_all()
    {
        if (IS_AJAX) {
            $ids = I('post.ids');
            foreach ($ids as $v) {
                $this->product->where("id = '$v'")->delete();
            }
        }
    }

    /***
     * ajax单选删除
     */
    public function delete_one()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $this->product->where("id = '$id'")->delete();
        }
    }

    /***
     * ajax单条还原
     */
    public function restore()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $this->product->where("id = '$id'")->setField("status", 1);
        }
    }

    /***
     * ajax单条回收
     */
    public function recycle_one()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $this->product->where("id = '$id'")->setField("status", 0);
        }
    }

    /***
     * ajax排序
     */
    public function sort_order()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $sort_order = I('post.sort_order');
            $this->product->where("id = '$id'")->setField('sort_order', $sort_order);
        }
    }

    /***
     * ajax库存
     */
    public function stock()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $stock = I('post.stock');
            $this->product->where("id = '$id'")->setField('stock', $stock);
        }
    }

    /***
     * 是否显示
     */
    public function change_attr()
    {
        if (IS_AJAX) {

            $id = I('post.id');
            $is_all = I('post.is_all');
            $_self = !I('post._self');
            $this->product->where("id = '$id'")->setField($is_all, $_self);
        }
    }

}
